/**
 * 
 */
package com.inspur.cloud.demo.controller;

import com.inspur.cloud.demo.entity.FlinkConfig;
import com.inspur.cloud.demo.service.flinksql.FlinkSqlService;
import com.inspur.cloud.demo.util.Response;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/paragraph")
@Api(tags="paragraph 操作接口")
public class ParagraphController {
    @Autowired
    FlinkSqlService flinkSqlService;
    @PostMapping("/add")
    @ApiOperation(value = "新增段落", notes = "新增段落")
    @ApiImplicitParam(name = "paramInfo" ,dataType="Map", paramType = "body",value="{\"title\":\"title\",\"text\":\"text\",\"notebookid\":\"noteid\"}",examples = @Example({
            @ExampleProperty(value = "{'title':'title','text':'text','notebookid':'noteid'}")
    }))
    public Response<?> addParagraph(@NotNull(message = "语句不能为空") @RequestBody Map paramInfo) throws Exception {
        HashMap<String,Object> param  = new HashMap<>();
        HashMap<String,Object> params  = new HashMap<>();
        String notebookid = (String)paramInfo.get("notebookid");//
        String text = (String)paramInfo.get("text");

        if(text!=null&& !text.equals("")) {
            params.put("title", paramInfo.get("title"));//标题
            params.put("text", paramInfo.get("text"));//内容
            param.put("params",params);
            return flinkSqlService.addParagraph(notebookid,param);
        }else
        {
            return Response.success("text is null!");
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存段落", notes = "保存段落")
    @ApiImplicitParam(name = "paramInfo" ,dataType="Map", paramType = "body",value="{\"text\":\"text\",\"notebookid\":\"noteid\",\"paragraphsid\":\"paragrahid\"}",examples = @Example({
            @ExampleProperty(value = "{'text':'text','notebookid':'noteid','paragraphsid':'paragh'}")
    }))
    public Response<?> saveParagraph(@NotNull(message = "语句不能为空") @RequestBody Map paramInfo) throws Exception {

        HashMap<String,Object> param  = new HashMap<>();
        HashMap<String,Object> params  = new HashMap<>();
        String notebookid = (String)paramInfo.get("notebookid");//
        String paragraphsid = (String)paramInfo.get("paragraphsid");//
        String text = (String)paramInfo.get("text");

        if(text!=null&& !text.equals("")) {
            params.put("title", paramInfo.get("title"));//sql为固定格式  //后期
            params.put("text", paramInfo.get("text"));//sql为固定格式  //后期
            param.put("params",params);
            return flinkSqlService.saveParagraph(notebookid,paragraphsid,param);
        }else
        {
            return Response.success("text is null!");
        }
    }


    @PostMapping("/delete")
    @ApiOperation(value = "删除段落", notes = "删除段落")
    @ApiImplicitParam(name = "paramInfo" ,dataType="Map", paramType = "body",value="{\"notebookid\":\"notebookid\",\"paragraphsid\":\"paragraphsid\"}",examples = @Example({
            @ExampleProperty(value = "{'notebookid':'notebookid','paragraphsid':'paragraphsid'}")
    }))
    public Response<?> deleteParagraph(@NotNull(message = "语句不能为空") @RequestBody Map paramInfo) throws Exception {
        String notebookid = (String)paramInfo.get("notebookid");//
        String paragraphsid = (String)paramInfo.get("paragraphsid");//
        return flinkSqlService.deleteParagraph(notebookid,paragraphsid);
    }
}
